import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;
import java.util.Random;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;
import java.util.Random;
public class stuInfo {
        public static void main(String[] args) {

            Properties properties = new Properties();
            properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.235.128:9092");
            properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
            properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");

            while (true) {

                KafkaProducer<String, String> kafkaProducer = new KafkaProducer<String,String>(properties);

                try {

                    for (int i = 0; i < 5; i++) {
                        Random random = new Random();

                        // 生成班级号（这里简单示例为1到10的随机数）
                        int classNumber = random.nextInt(10) + 1;
                        String className = "Class" + classNumber;

                        // 生成姓名（简单示例为固定几个名字中随机选）
                        String[] nameArray = new String[]{"Jack2", "Jesson3", "Megan5", "Bob4", "Amy1"};
                        int nameIndex = random.nextInt(nameArray.length);
                        String name = nameArray[nameIndex];

                        // 生成性别a
                        int gender = random.nextInt(2) + 1;

                        // 生成出生年月日（这里简单示例为固定格式的随机日期）
                        int year = random.nextInt(2023 - 2018 + 1) + 2018;
                        int month = random.nextInt(12) + 1;
                        int day = random.nextInt(28) + 1;
                        String birthDate = year + "-" + month + "-" + day;

                        // 生成学号（这里简单示例为10000到99999的随机数）
                        int studentId = random.nextInt(90000) + 10000;

                        // 生成学期
                        int semester = random.nextInt(8) + 1;

                        // 生成在籍状态
                        String status = random.nextInt(2) == 0? "L" : "A";

                        // 按照要求拼接数据，用制表符\t作为分隔符
                        String mockData = className + "\t" + name + "\t" + gender + "\t" + birthDate + "\t" + studentId + "\t" + semester + "\t" + status;
                        System.out.println("" + mockData);

                        kafkaProducer.send(new ProducerRecord<String,String>("Project", mockData));
                    }

                    try {
                        Thread.sleep(2000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    kafkaProducer.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

